package com.hmall.service;

import com.hdmall.business.common.common.ServerResponse;
import com.hmall.mapper.*;
import com.hmall.model.*;
import com.hmall.util.CastHotUtil;
import com.hdmall.business.common.util.SimilarUtil;
import com.hmall.vo.PageVo;
import com.hmall.mapper.CmsArticleInfoMapper;
import com.hmall.mapper.MallAdvertMapper;
import com.hmall.mapper.MallHistoryMapper;
import com.hmall.mapper.MallProductMapper;
import com.hmall.model.CmsArticleInfo;
import com.hmall.model.MallAdvert;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MobileHomeService {

    private final
    MallProductMapper mallProductMapper;

    private final
    CmsArticleInfoMapper cmsArticleInfoMapper;

    private final
    MallHistoryMapper mallHistoryMapper;

    private final
    MallAdvertMapper mallAdvertMapper;

    private final
    MallOrderItemMapper mallOrderItemMapper;

    private final
    MallBannerMapper mallBannerMapper;

    private final
    MallHotMapper mallHotMapper;

    @Autowired
    public MobileHomeService(MallProductMapper mallProductMapper, CmsArticleInfoMapper cmsArticleInfoMapper, MallHistoryMapper mallHistoryMapper, MallAdvertMapper mallAdvertMapper, MallOrderItemMapper mallOrderItemMapper, MallBannerMapper mallBannerMapper, MallHotMapper mallHotMapper) {
        this.mallProductMapper = mallProductMapper;
        this.cmsArticleInfoMapper = cmsArticleInfoMapper;
        this.mallHistoryMapper = mallHistoryMapper;
        this.mallAdvertMapper = mallAdvertMapper;
        this.mallOrderItemMapper = mallOrderItemMapper;
        this.mallBannerMapper = mallBannerMapper;
        this.mallHotMapper = mallHotMapper;
    }

    //截取count 条
    //热销业务
    public ServerResponse<List<MallHotVo>> listHotProduct(int count) {

        List<MallHot> mallHotList = mallHotMapper.listOnlyHotList(count);

        //就6条
        List<Integer> mallProductIdList = new ArrayList<>();
        for(MallHot mallHot : mallHotList) {
            mallProductIdList.add(mallHot.getProductId());
        }


        List<MallProduct> hotProductList = mallProductMapper.selectProductList(mallProductIdList);
        List<MallHotVo> mallHotVoList = new ArrayList<>();
        for(int i = 0; i < hotProductList.size(); i++){
            mallHotVoList.add(CastHotUtil.hotCast(hotProductList.get(i), mallHotList.get(i).getSalesVolume()));
        }




        return ServerResponse.createBySuccess(mallHotVoList);
    }
    public List<CmsArticleInfo> listAricleInfo(Date startTime, Date endTime) {

        List<CmsArticleInfo> cmsArticleInfos = cmsArticleInfoMapper.listAricleInfo(startTime, endTime);
        return null;
    }

    //列出某个具体用户的购买过的产品id
    public List<Integer> listHistoryProductIdByUserId(Integer userId) {
        int maxCount = 3;

        return mallHistoryMapper.selectByUserIdByCreateTime(userId, maxCount);
    }

    //根据产品id，找出同类型的产品，对于每个类型都返回指定的个数,然后去重返回
    public Set<Integer> listCategoryIdByProductId(List<Integer> historyProductList){

        Set<Integer> categoryIdSet = new HashSet<>();
        for(Integer productId: historyProductList){
            int categoryId = mallProductMapper.selectCategoryByProductId(productId);
            categoryIdSet.add(categoryId);
        }

        return categoryIdSet;
    }

    /**
     * 猜你喜欢算法:
     *  A对Y的喜好程度 = 求和（A买了X * X与Y的相似度）
     *  https://www.jianshu.com/p/d03fb6d68716
     * @param userId
     * @param pageNum
     * @param pageSize
     * @return
     */
    public ServerResponse<PageVo<MallProduct>> listUserLike(int userId, int pageNum, int pageSize){

        //A买了X记为1 不买记为2   X与Y的相似度通过name进行匹配 属性 30%，光泽 20%，纤度/孔数 20%
        // 单合股 10%颜色 10%形状 10%

        //一、列出用户已经购买的产品  查询订单明细表
        List<Integer> buyProductIdList = mallOrderItemMapper.findProductListByUser(userId);
        //二、列出所有公司生产的产品
        List<MallProduct> sellProductList = mallProductMapper.selectList();

        //省去查数据库
        Map<Integer, MallProduct> productMap = new HashMap<>();
        for(MallProduct mallProduct : sellProductList){
            productMap.put(mallProduct.getId(), mallProduct);
        }


        Map<MallProduct, Integer> likeMap = new HashMap<>();

        //保证有映射关系
        for(MallProduct sellProduct: sellProductList){
            likeMap.put(sellProduct, 0);
        }
        //相似度求和计算
        for(MallProduct sellProduct : sellProductList){
            String sellName = sellProduct.getName();
            for(Integer buyProductId : buyProductIdList){
                //如果是同一件就放弃
                if(sellProduct.getId().equals(buyProductId)) continue;
                MallProduct mallProduct = productMap.get(sellProduct.getId());
                String buyName = mallProduct.getName();
                //相似度计算公式
                int result = SimilarUtil.similarValue(sellName, buyName);
                int value = likeMap.get(mallProduct);
                likeMap.put(mallProduct, value + result);
            }
        }

        //进行排序
        List<Map.Entry<MallProduct,Integer>> list = new ArrayList<>(likeMap.entrySet());
        list.sort((o1, o2) -> o2.getValue() - o1.getValue());

        //转换成集合
        List<MallProduct> mallProductList = new ArrayList<>();
        for(Map.Entry<MallProduct, Integer> t: list){
            mallProductList.add(t.getKey());
        }

        List<MallProduct> subMallProductList = new ArrayList<>();


        int pageStart = pageSize * (pageNum - 1);

        int total = mallProductMapper.listUserLikeCount();

        //假分页，子集合
        for(int i = pageStart; i < pageSize + pageStart; i++){
            if(i >= total) break;
            subMallProductList.add(mallProductList.get(i));
        }

        PageVo<MallProduct> pageVo = new PageVo<>(pageNum, pageSize, total, subMallProductList );

        return ServerResponse.createBySuccess(pageVo);

    }

        //轮播图，默认四个
    public List<MallAdvert> listAdvert(int advertCount) {

        List<MallAdvert> mallAdvertList = new ArrayList<>();
        mallAdvertList = mallAdvertMapper.listAdvert(advertCount);
        return mallAdvertList;
    }

    public List<MallAdvert> listHomeAdvert(int advertCount, int i) {

        List<MallAdvert> mallAdvertList = new ArrayList<>();
        mallAdvertList = mallAdvertMapper.listHomeAdvert(advertCount, i);
        return mallAdvertList;
    }

    public List<MallAdvert> listMeAdvert(int advertCount, int i) {

        List<MallAdvert> mallAdvertList = new ArrayList<>();
        mallAdvertList = mallAdvertMapper.listMeAdvert(advertCount, i);
        return mallAdvertList;
    }

    public ServerResponse<List<MallBanner>> listHomeBanner() {
        List<MallBanner> mallBannerList = mallBannerMapper.listPCBanner();
        return ServerResponse.createBySuccess(mallBannerList);
    }

    public ServerResponse<List<MallBanner>> listMeBanner(String me_banner) {
        System.out.println(me_banner);
        List<MallBanner> mallBannerList = mallBannerMapper.listByType(me_banner);
        return ServerResponse.createBySuccess(mallBannerList);
    }




}


